package com.example.androidthings.myproject.server.httpapi;

import android.util.Log;

import com.yanzhenjie.andserver.RequestHandler;
import com.yanzhenjie.andserver.util.HttpRequestParser;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class RequestCarControlHandler implements RequestHandler {
    private static final String TAG = "RequestCarControlHandle";
    @Override
    public void handle(HttpRequest req, HttpResponse res, HttpContext con) throws UnsupportedEncodingException {
        Map<String, String> params = null;
        try {
            params = HttpRequestParser.parse(req);
        } catch (IOException e) {
            Log.e(TAG, "handle: IOException");
            e.printStackTrace();
        }
        String action = params.get("action");
        StringEntity stringEntity =null;
        switch (action){
            case "forward":
                EventBus.getDefault().post("forward");
                stringEntity = new StringEntity("forward", "utf-8");
                break;
            case "back":
                EventBus.getDefault().post("back");
                stringEntity = new StringEntity("back", "utf-8");
                break;
            case "left":
                EventBus.getDefault().post("left");
                stringEntity = new StringEntity("left", "utf-8");
                break;
            case "right":
                EventBus.getDefault().post("right");
                stringEntity = new StringEntity("right", "utf-8");
                break;
            default:
                stringEntity = new StringEntity("un know action", "utf-8");
        }
        res.setEntity(stringEntity);
        /*// Request params.
        String userName = params.get("action");
        String password = params.get("password");

        if ("123".equals(userName) && "123".equals(password)) {
            StringEntity stringEntity = new StringEntity("Login Succeed", "utf-8");
            res.setEntity(stringEntity);
        } else {
            StringEntity stringEntity = new StringEntity("Login Failed", "utf-8");
            res.setEntity(stringEntity);
        }*/
    }
}